<?php
$GLOBALS['Md'] = "Support";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.php");
print heade();
print body();
module($BSD['DB00']);
office_header($_SESSION['office']);
include($GLOBALS['PAD']."Modules/Support/main.obj.php");
include($GLOBALS['PAD']."Modules/Support/nav.views.php");
print NavFront();
// SAVE TICKET
if (isset($_POST['SaveTicket'])) {
	if ($_POST['Area'] != "") {
	$timeCreation = date("Y-m-d H:i:s");
	 if ($_POST['date_occur'] != "AAAAMMDD") {

	$dateocAno = substr($_POST['date_occur'],0,4);
	$dateocMes = substr($_POST['date_occur'],4,2);
	$dateocDia = substr($_POST['date_occur'],6,2);
	$timeOccur = $dateocAno."-".$dateocMes."-".$dateocDia." ".$_POST['time_occur'].":00";

	} else {
	$timeOccur = $timeCreation;
	}

	$QAttrib = "SELECT * FROM administration_it WHERE refArea = '{$_POST['Area']}'";
	$Attrib = $GLOBALS['CONN']->db_array($QAttrib);

	$CSave = "INSERT INTO it_tickets (main_users_ref, main_domain_ref, main_office_ref, Description, ref_type, time_creation, priority, status, time_occur, refUserAtrib)
	VALUES ('{$_SESSION['UtID']}','1','{$_SESSION['office']}','{$_POST['Description']}','{$_POST['Area']}','{$timeCreation}','1','1','{$timeOccur}','{$Attrib[1]['refUserAtrib']}')";
	$Save = $GLOBALS['CONN']->db_query($CSave);
	$Msg->SAVE_CLIENT = "Ticket Enviado.";
	} else {
	$Msg->SAVE_CLIENT = "Preencha todos os campos.";
	}
}
?>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/Objects/slide.js"></script>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/JS/calendar.js"></script>




<table width="100%"  border="0" cellpadding="10">
  <?php if (isset($_GET['action']) && $_GET['action'] == "show" || !isset($_GET['action'])) { ?>
<tr>
    <td>
	<table width="100%"  border="0" cellpadding="10" cellspacing="2">
      <tr bgcolor="#009999">
        <td colspan="3">Suporte IT  | Estado de Tickets</td>
      </tr>
      <tr>
        <td width="20%" rowspan="3" valign="top" bgcolor="#F2F2F2"></p></td>
        <td colspan="2" bgcolor="#F2F2F2">
	<table width="100%" border="0" cellpadding="4" cellspacing="1">
		<?php
				$CTickets = "SELECT
							it_tickets.ref,
							it_tickets.main_users_ref AS UsersRef,
							 it_tickets.time_creation,
							  it_tickets.Description,
							   it_tickets.refUserAtrib,
							    it_tickets.status,
							    it_tickets.time_solve,
								it_status.name AS Status,
								 main_users.ref AS MainUsersRef,
								 main_users.name AS UserName
								FROM it_tickets
								INNER JOIN main_users ON main_users.ref = it_tickets.main_users_ref
								INNER JOIN it_status ON it_status.ref = it_tickets.status
								WHERE main_users_ref = '{$_SESSION['UtID']}'";
				$Tickets = $GLOBALS['CONN']->db_array($CTickets);
				for ($t = 1; $t <= $Tickets[0]; $t++) { ?>
      <tr bgcolor="#E0E1D5">
        <td width="30%"><a href="index.php?action=show&Ticket=<?=$Tickets[$t]['ref'];?>">Aberto a: <?=$Tickets[$t]['time_creation'];?></a></td>
        <td width="30%">Situação: <?=substr($Tickets[$t]['Description'],0,30);?></td>
        <td width="30%">Estado: <?=$Tickets[$t]['Status'];?></td>
      </tr>
	  <?php if (isset($_GET['Ticket']) && $_GET['Ticket'] == $Tickets[$t]['ref']) { ?>
	  <tr bgcolor="#FFFFFF">
	    <td colspan="2"><strong>Descrição</strong><br>
		<?=$Tickets[$t]['Description'];?></td>
	    <td><strong>Responsável</strong><br>
	      <?php
		  					$CAtribs = "SELECT ref,name
										FROM main_users
										WHERE ref = {$Tickets[$t]['refUserAtrib']}";
							$Atribs = $GLOBALS['CONN']->db_array($CAtribs);
		  					print $Atribs[1]['name'];?>	      </td>
	    </tr>
	<?php if ($Tickets[$t]['status'] == '3' || $Tickets[$t]['status'] == '5') { ?>
	  <tr bgcolor="#FFFFFF">
	    <td colspan="2"><strong>Resolução</strong><br>
		<?=$Tickets[$t]['Response'];?></td>
	    <td>Data de resolução<br>
	      <?=$Tickets[$t]['time_solve'];?></td>
	    </tr>
	  <?php }
	  }
	  } ?>
    </table>
	</td></tr>
    </table>
	</td>
  </tr>
  <?php } elseif (isset($_GET['action']) && $_GET['action'] == "new") { ?>
  <tr>
    <td>
	<table width="100%"  border="0" cellpadding="10" cellspacing="2">
	<form action="index.php?action=done" method="post">
      <tr bgcolor="#009999">
        <td colspan="3">Suporte IT  | Abrir Ticket </td>
      </tr>
	  <?php if(!isset($print_Var)) { ?>
      <tr>
        <td width="25%" rowspan="3" valign="top" bgcolor="#F2F2F2"> <p><b>&lt;&gt;</b><br>
            Tipifique e descreva a situação o mais promonorizadamente possivel. </p>
          <p><?php if (isset($Msg->SAVE_CLIENT)) {print $Msg->SAVE_CLIENT;} ?>
            Aberto por:
            <?=$_SESSION['UtAD'];?>
</p>
          <p>A:
            <?=date("Y-m-d H:i:s");?>
</p>
          <p><strong>Data da ocorrência</strong>:<br>
            <input name="date_occur" type="text" id="date_occur2"
		  onFocus="this.select();lcs(this)"
		  onClick="event.cancelBubble=true;this.select();lcs(this)"
		  value="<?php if (isset($_POST['date_occur'])) {
		  print $_POST['date_occur']; } else { print "AAAAMMDD";
		  } ?>" size="10" />
            <input name="time_occur" type="text" id="time_occur2" value="00:00" size="2" maxlength="5">
</p>
          <p>(apenas se diferente)           </p></td>
        <td colspan="2" bgcolor="#F2F2F2">
	<table width="100%" border="0" cellpadding="10" cellspacing="1">
      <tr bgcolor="#FFFFFF">
        <td width="10%" rowspan="2" valign="top">Tipificação</td>
        <td>área </td>
        <td><select name="Area" onclick="javascript:window.refresh();">
          <?php
				$CType = "SELECT * FROM it_ticket_types WHERE level = '0'";

				$Type = $GLOBALS['CONN']->db_array($CType);
				for ($t = 1; $t <= $Type[0]; $t++) { ?>
          <option value = "<?=$Type[$t]['ref'];?>">
          <?=$Type[$t]['name'];?>
          </option>
          <?php } ?>
        </select></td>
        </tr>
      <tr>
        <td width="10%" bgcolor="#FFFFFF">Sub-área</td>
        <td width="70%" bgcolor="#FFFFFF"><p>
          <select name="SubArea">
                <?php
				$CType = "SELECT * FROM it_ticket_types WHERE level = '{$_POST['Area']}'";
				$Type = $GLOBALS['CONN']->db_array($CType);
				for ($t = 1; $t <= $Type[0]; $t++) { ?>
                <option value = "<?=$Type[$t]['ref'];?>">
                <?=$Type[$t]['name'];?>
                </option>
                <?php } ?>
          </select>
        </p></td>
        </tr>
      <tr bgcolor="#FFFFFF">
        <td valign="top">Descrição</td>
        <td colspan="2" valign="top"><p>
            <textarea name="Description" cols="80" rows="8" id="Description"></textarea>
        </p>          </td>
        </tr>
      <tr>
        <td valign="top">&nbsp;</td>
        <td colspan="2" valign="top"><input name="SaveTicket" type="submit" id="SaveTicket2" value="Salvar"></td>
      </tr>
    </table>
	<?php } ?>
	</td>
	  </tr>
	  </form>
    </table>
	</td>
  </tr>
  <?php } elseif (isset($_GET['action']) && $_GET['action'] == "done") { ?>
    <tr>
    <td>
	<table width="100%"  border="0" cellpadding="10" cellspacing="2">
	<form action="index.php?action=done" method="post">
      <tr bgcolor="#009999">
        <td colspan="3">Suporte IT  | Ticket a aguardar resolução </td>
      </tr>
	  <?php if(!isset($print_Var)) { ?>
      <tr>
        <td width="20%" rowspan="3" valign="top" bgcolor="#F2F2F2"> <p><b>&lt;&gt;</b><br>
            </p>
          <p><?php if (isset($Msg->SAVE_CLIENT)) {print $Msg->SAVE_CLIENT;} ?></p></td>
        <td colspan="2" valign="top" bgcolor="#F2F2F2">
	      <p>
	          <?php } ?>
	  Ticket enviado.</p>
	      <p>Confirme a resolução através da vista de consulta de tickets. </p></td>
	  </tr>
	  </form>
    </table>
	</td>
  </tr>
  <?php } ?>
</table>
<?php print footer(); ?>